//
//  AppDelegate.swift
//  ZWWEIBO
//
//  Created by xzw on 2017/11/17.
//  Copyright © 2017年 xzw. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       
        setupAddtions() //设置应用额外信息

        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        
        loadAppInfo() //加载网络数据
        
        return true
    }

}

 //MARK: - 设置应用程序额外信息
extension AppDelegate {
    
    private func setupAddtions(){
        
        //1.设置指示器提醒框消失时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        //2.设置afn网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //3.用户授权通知，包括提醒、声音、图标
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.sound,.badge,.carPlay]) { (isSuccess, error) in
                print("授权通知 \(isSuccess ? "成功" : "失败")")
            }
        } else {
            let notificationSetting = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notificationSetting)
        }
    }
    
}


 //MARK: - 加载网络可配置的json
extension AppDelegate{
    
    private func loadAppInfo(){
        
        DispatchQueue.global().async {
            
            //1.url
            let url = Bundle.main.url(forResource: "main.json", withExtension: nil)
            
            //2.data
            let data = NSData(contentsOf: url!)
            
            //3.写入磁盘
            let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (documentPath as NSString).appendingPathComponent("main.json")
            data?.write(toFile: jsonPath, atomically: true)
            
            print("应用程序加载完毕 \(jsonPath)")
        }
        
    }
}
